﻿Public Class frmPassword
    Dim cambiar As Boolean = False
    Dim _formPrincipal As frmPrincipal

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByRef formPrincipal As frmPrincipal)
        InitializeComponent()
        _formPrincipal = formPrincipal
    End Sub

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        cambiar = False
        Me.Close()
    End Sub

    Private Sub cmdGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGuardar.Click
        If Me.txtactual.Text.Trim() = "" Then
            MsgBox("La Contraseña Actual es requerida.", vbOKOnly + vbCritical, "Atención")
            Me.txtactual.Focus()
            Exit Sub
        End If
        If Me.txtnueva1.Text.Trim() = "" Then
            MsgBox("La Nueva Contraseña es requerida.", vbOKOnly + vbCritical, "Atención")
            Me.txtnueva1.Focus()
            Exit Sub
        End If
        If Me.txtnueva2.Text.Trim() = "" Then
            MsgBox("La Repetición de la Nueva Contraseña es requerida.", vbOKOnly + vbCritical, "Atención")
            Me.txtnueva2.Focus()
            Exit Sub
        End If
        If Me.txtnueva1.Text.Trim() = Me.txtactual.Text.Trim() Then
            MsgBox("La Contraseña Actual y la Nueva Contraseña son iguales.", vbOKOnly + vbCritical, "Atención")
            Me.txtnueva1.Focus()
            Exit Sub
        End If
        If Me.txtnueva1.Text.Trim() <> Me.txtnueva2.Text.Trim() Then
            MsgBox("La Nueva Contraseña y su Repetición no son iguales.", vbOKOnly + vbCritical, "Atención")
            Me.txtnueva1.Focus()
            Exit Sub
        End If


        Dim myAccesoDatos As New AccesoDatos
        Dim myConsulta As String

        myConsulta = _
            "SELECT ID FROM EMPLEADO " & _
            "WHERE ACTIVO = 'S' AND CONTRASENYA_LOGIN = '" & Me.txtactual.Text.Trim() & "' AND ID = " & Me._formPrincipal.idUsuarioLogueado

        Dim table As Data.DataTable
        table = myAccesoDatos.ejecutarConsulta(myConsulta)

        If table.Rows.Count > 0 Then
            myConsulta = _
            "UPDATE EMPLEADO " & _
            "SET CONTRASENYA_LOGIN = '" & Me.txtnueva1.Text.Trim() & "' WHERE ID = " & Me._formPrincipal.idUsuarioLogueado
            myAccesoDatos.ejecutarConsulta(myConsulta)
            cambiar = True
            MsgBox("La Nueva Contaseña se ha Guardado correctamente.", vbOKOnly + MsgBoxStyle.Information, "Atención")
            Me.Close()
        Else
            MsgBox("La Contaseña Actual es Incorrecta.", vbOKOnly + vbCritical, "Atención")
        End If

    End Sub

    Private Sub txtactual_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtactual.KeyPress
        If Asc(e.KeyChar) = 32 Then
            e.KeyChar = ""
            Exit Sub
        End If
    End Sub
    Private Sub txtnueva1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnueva1.KeyPress
        If Asc(e.KeyChar) = 32 Then
            e.KeyChar = ""
            Exit Sub
        End If
    End Sub
    Private Sub txtnueva2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnueva2.KeyPress
        If Asc(e.KeyChar) = 32 Then
            e.KeyChar = ""
            Exit Sub
        End If
    End Sub

    Private Sub frmPassword_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If cambiar = False Then
            If MessageBox.Show("Si cancela no se guardarán los cambios ¿Está seguro?", "Atención", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                e.Cancel = False
            Else
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub frmPassword_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class